package com.yfqy.app.modules.order.vo;

import com.yfqy.app.modules.order.dto.OrderInfoDTO;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单创建结果
 */

@Data
public class OrderCreateResultVO {

    /**
     * 订单ID
     */
    private Long orderId;

    /**
     * 订单编号
     */
    private String orderNo;

    /**
     * 订单状态(0-待付款；1-已付款，待发货；2-支付超时等)
     */
    private Byte orderStatus;

    /**
     * 订单总金额
     */
    private BigDecimal totalAmount;

    /**
     * 实付金额
     */
    private BigDecimal paymentAmount;

    /**
     * 优惠金额
     */
    private BigDecimal discountAmount;

    /**
     * 积分抵扣金额
     */
    private BigDecimal pointAmount;

    /**
     * 运费
     */
    private BigDecimal freightAmount;

    /**
     * 订单类型(1-普通，2-满减，3-折扣，4-特价，5-拼团)
     */
    private Byte orderType;

    /**
     * 支付超时时间
     */
    private Date payExpireTime;


    // ============ 构造方法 ============

    public OrderCreateResultVO() {
    }

    public static OrderCreateResultVO success(OrderInfoDTO orderInfoDTO) {
        OrderCreateResultVO result = new OrderCreateResultVO();
        result.setOrderId(orderInfoDTO.getId());
        result.setOrderNo(orderInfoDTO.getOrderNo());
        result.setOrderStatus(orderInfoDTO.getOrderStatus());
        result.setTotalAmount(orderInfoDTO.getTotalAmount());
        result.setPaymentAmount(orderInfoDTO.getPaymentAmount());
        result.setDiscountAmount(orderInfoDTO.getDiscountAmount());
        result.setPointAmount(orderInfoDTO.getPointAmount());
        result.setFreightAmount(orderInfoDTO.getFreightAmount());
        result.setOrderType(orderInfoDTO.getOrderType());
        result.setPayExpireTime(orderInfoDTO.getPayExpireTime());

        return result;
    }

    private static Date calculatePayExpireTime() {
        // 默认30分钟后支付超时
        return new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    }

    private static Long calculateGroupRemainTime(Date expireTime) {
        if (expireTime == null) {
            return 0L;
        }
        long remain = expireTime.getTime() - System.currentTimeMillis();
        return remain > 0 ? remain / 1000 : 0;
    }

}
